<!-- 我的日记 主诉页面 -->

<template>
	<view class="container">
		<lk-topbar :title="'主诉'" :backcolor="'#f5f5f5'"  @back="back"></lk-topbar>
		<view class="p30">
			<!-- 引入记录组件 -->
			<lk-record-dsym :types="'pres'"  @delectTap="delectTap" @change='change' :lists='symptom' @selectMoment="selectMoment" ></lk-record-dsym>
		</view>
		<view class="btn w75 h84 mauto radius50 mb20" @click="save()"> 打卡</view>
		
	</view>
</template>

<script>
	import lkRecordDsym from '../../components/lk-record-dsym/index.vue'
	export default {
		name:"",
		data() {
			return {
				date:'',
				symptom: [],//我的主诉
				isSave:true,
				type:'',
				time:"",
				oldsymptom:[],
				isClick:true
			}
		},
		props:{
			
		},
		components:{
			lkRecordDsym
		},
		onLoad(o) {
			this.date	= this.$utils.getTime(new Date(),'Y-M-D');
			this.type = o.type;
			this.time = o.time;
			if(o.type == 'detail'){
				this.getDetail();
			}else{
				this.getUserDetail()
				this.getDetail();
			}
		},
		onShow() {
			let pages =getCurrentPages();
			let cur = pages[pages.length -1];
			if(typeof cur.data.med != 'undefined'){
				this.isSave = false;
				if(cur.data.cindex != -1){
					this.symptom[cur.data.pindex].symptom.splice(cur.data.cindex,1,cur.data.med);
				}else{
					this.symptom[cur.data.pindex].symptom.push(cur.data.med)
				}
			}
		},
		methods: {
			// 保存
			save(){
				let _this = this;
				if(_this.isClick){
					_this.isClick = false
					if(!_this.symptom.every(item=>item.time != '')){
						wx.showToast({
							title:"请选择时间",
							icon:'none'
						})
						_this.isClick = true;
						return false;
					}
					if(_this.type != 'detail'){
						_this.oldsymptom = _this.oldsymptom.concat(_this.symptom)
					}
					 _this.$utils.request({
						 url:"/patient/diary/",
						 method:"POST",
						 data:{
							 date:_this.date,
							 symptom:_this.oldsymptom,
						 }
					 }).then(res=>{
						 wx.showToast({
							 title:"保存成功！"
						 })
						 this.isSave =true;
						 setTimeout(()=>{
							this.$routeJump.back()
							_this.isClick = true
						 },2000)
					 })
				}
			},
			//请求症状
			getDetail(){
				let _this = this;
				 _this.$utils.request({
					 url:"/patient/diary/",
					 data:{
						 date:_this.date
					 }
				 }).then(res=>{
					 if(_this.type == 'detail'){
						 res.data.symptom.forEach(item=>{
							 item = Object.assign(item,{
							 	change:0
							 })
						 	if(item.time == _this.time){
						 		_this.symptom = [item]
						 	}
						 })
					 }
					 
					  _this.oldsymptom = res.data.symptom;
				 })
			},
			getUserDetail(){
				let _this = this;
				 _this.$utils.request({
					 url:"/patient/user_info/",
				 }).then(res=>{
						res.data.symptom.forEach(item=>{
							item = Object.assign(item,{
								change:0
							})
						})
						_this.symptom = [].concat([],[{
							time:this.$utils.getTime(new Date(), 'h:m'),
							symptom:res.data.symptom
						}]);
				 })
			},	
				/** 修改程度
				 * @param {Object} e
				 */
			selectMoment(e){
				this.symptom[e.index].symptom[e.ind].change = e.num;
				this.isSave = false;
			},
			
			/** 修改时间
			 * @param {Object} e 返回的参数
			 */
			change(e){
				this.symptom[e.i].time = e.time;
				this.isSave = false;
			},
			
			/**
			 * 删除某条数据
			 * @param {Object} o 返回数据
			 */
			delectTap(o){
				this.isSave  =false;
				this.symptom[o.index].symptom.splice(o.ind,1)
			},
			
			// 页面返回
			back(){
				let _this =this;
				if(_this.isSave){
					_this.$routeJump.back()
				}else{
					wx.showModal({
						title:"提示",
						content:"你还没保存，是否保存？",
						success(res){
							if(res.confirm){
								_this.save()
							}else{
								_this.$routeJump.back()
							}
						}
					})
				}
			},
		}
	}
	
</script>

<style lang="scss" scoped>
	.container{
		background-color: #f5f5f5;
		
	}
	.btn{
		background-color: #6aa1a8 !important;
	}
</style>
